//: src/com/silvtar/io/StdOut.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 4/21/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * Modify History:
 * Date        Version    Description
 * 4/22/2013   0.1        Create The Class
 * 5/26/2013   0.2        Add gui terminal support
 */

package com.silvtar.io;

import com.silvtar.sync.ui.TerminalPanel;

/**
 * 标准输出类，为程序提供统一的输出接口。
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.2
 */
public class StdOut {

    static int           whitchTerm;
    static int           whitchLogTerm;
    static TerminalPanel term    = new TerminalPanel();
    static TerminalPanel logTerm = new TerminalPanel();

    // -------------------- getter & setter ---------------------------
    public static TerminalPanel getTerm() {
        return term;
    }
    
    public static TerminalPanel getLogTerm() {
        return logTerm;
    }

    public static int getWhichTerm() {
        return whitchTerm;
    }
    
    public static int getWhichLogTerm() {
        return whitchLogTerm;
    }
    
    public static void setTerm(TerminalPanel term) {
        StdOut.term = term;
    }
    
    public static void setLogTerm(TerminalPanel logTerm) {
        StdOut.logTerm = logTerm;
    }

    // --------------------------------------------------------------

    /**
     * 切换输出到图形终端
     */
    public static void chTerm() {
        whitchTerm = 1;
    }

    /**
     * 切换输出到控制台
     */
    public static void chStd() {
        whitchTerm = 0;
    }

    /**
     * 输出到错误日志。
     * 
     * @param ob
     *            一般为this，即类本身
     * @param string
     *            要输出的信息
     */
    public final static void printErrLog(Object ob, String string) {
        String className_S = ob.getClass().getName();
        String str = "WARRING!! " + className_S + ": " + string;

        switch (whitchTerm) {
        case 1:
            term.println(str);
            break;
        default:
            printErr(str);
            break;
        }

    }

    /**
     * 输出到警告日志。
     * 
     * @param ob
     *            一般为this，即类本身
     * @param string
     *            要输出的信息
     */
    public final static void printWarningLog(Object ob, String string) {
        String className_S = ob.getClass().getName();
        String str = "ERROR!! " + className_S + ": " + string;

        switch (whitchTerm) {
        case 1:
            term.println(str);
            break;
        default:
            printErr(str);
            break;
        }
    }

    /**
     * 打印字符串到标准错误输出。
     */
    public final static void printErr(String string) {

        switch (whitchTerm) {
        case 1:
            term.println(string);
            break;
        default:
            System.err.println(string);
            break;
        }
    }

    public final static void printLog(String string) {

        switch (whitchTerm) {
        case 1:
            term.println(string);
            break;
        default:
            System.err.println(string);
            break;
        }
    }

    /**
     * 打印字符串到outputDevice指定的设备上。
     */
    public final static void println() {
        switch (whitchTerm) {
        case 1:
            term.println();
            break;
        default:
            System.out.println();
            break;
        }
    }

    /**
     * 打印字符串到outputDevice指定的设备上。
     * 
     * @param string
     *            要输出的信息
     */
    public final static void println(String string) {
        switch (whitchTerm) {
        case 1:
            term.println(string);
            break;
        default:
            System.out.println(string);
            break;
        }
    }
}
